//
//  WYSettingController.m
//  CirclePush
//
//  Created by admin on 2018/4/17.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYSettingController.h"
#import "WYEditPassWordController.h"
#import "WYAboutController.h"
#import "WYLoginController.h"
@interface WYSettingController ()

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *topHeight;
@property (nonatomic,strong)NSString *fileCachePath;

@property (strong, nonatomic) IBOutlet UILabel *sizeLabel;


@end

@implementation WYSettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}
-(void)setupUI
{
    [self initTopBarWithTitle:@"设置"];
    [self addLeftButton:[UIImage imageNamed:@"return"] width:0 height:0];
    self.topHeight.constant = NavHeight+10;
    self.view.backgroundColor = [UIColor hexStringToColor:@"#f4f4f4" alpha:1.0];
    self.sizeLabel.text = [NSString stringWithFormat:@"%.2fM",[self getCacheSize]];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)editPassWord:(id)sender {
    WYEditPassWordController *epc = [[WYEditPassWordController alloc]init];
    [self.navigationController pushViewController:epc animated:YES];
    NSLog(@"111111111111111");
}

- (IBAction)aboutMe:(id)sender {
    WYAboutController *avc = [[WYAboutController alloc]init];
    [self.navigationController pushViewController:avc animated:YES];
    NSLog(@"2222222222222222");
}

- (IBAction)clearSize:(id)sender {
    [self cleanCache];
    NSLog(@"33333333333333333");
}

- (IBAction)quitLogin:(id)sender {
    WYLoginController *loginVc = [[WYLoginController alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:loginVc];
    [WYStoreModule cleanToken];
    [self presentViewController:nav animated:YES completion:nil];
    
    
    
}
-(CGFloat) getCacheSize
{
    //获取文件管理器对象
    NSFileManager * fileManger = [NSFileManager defaultManager];
    
    //获取缓存沙盒路径
    NSString * cachePath =  [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    
    //拼接缓存文件夹路径
    NSString * fileCachePath = [cachePath stringByAppendingPathComponent:@""];
    
    //将缓存文件夹路径赋值给成员属性(后面删除缓存文件时需要用到)
    self.fileCachePath = fileCachePath;
    
    //通过缓存文件路径创建文件遍历器
    NSDirectoryEnumerator * fileEnumrator = [fileManger enumeratorAtPath:fileCachePath];
    
    //先定义一个缓存目录总大小的变量
    NSInteger fileTotalSize = 0;
    
    for (NSString * fileName in fileEnumrator)
    {
        //拼接文件全路径（注意：是文件）
        NSString * filePath = [fileCachePath stringByAppendingPathComponent:fileName];
        
        //获取文件属性
        NSDictionary * fileAttributes = [fileManger attributesOfItemAtPath:filePath error:nil];
        
        //根据文件属性判断是否是文件夹（如果是文件夹就跳过文件夹，不将文件夹大小累加到文件总大小）
        if ([fileAttributes[NSFileType] isEqualToString:NSFileTypeDirectory]) continue;
        
        //获取单个文件大小,并累加到总大小
        fileTotalSize += [fileAttributes[NSFileSize] integerValue];
    }
    
    //将字节大小转为MB，然后传出去
    return fileTotalSize/1000.0/1000;
}
-(void) cleanCache
{
    //删除缓存目录下所有缓存文件
    [[NSFileManager defaultManager] removeItemAtPath:self.fileCachePath error:nil];
    self.sizeLabel.text = [NSString stringWithFormat:@"%.2fM",[self getCacheSize]];
}

@end
